package com.xbdun.web.bmp.controller.system;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.constant.XbdunLoginConst;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.request.system.SysMenuRequest;
import com.xbdun.facade.bmp.result.system.MenuPlatResult;
import com.xbdun.facade.bmp.result.system.PlatListResult;
import com.xbdun.facade.bmp.result.system.SysMenuResult;
import com.xbdun.facade.bmp.result.system.TreeNodeResult;
import com.xbdun.facade.bmp.service.system.SysMenuFacade;

import lombok.extern.slf4j.Slf4j;

/**
 * 菜单管理
 * @Description: 平台对菜单的管理
 * @author  伍文强
 * @Date 2017年5月12日10:05:21
 */
@Controller
@Slf4j
@XbdunLogin(XbdunLoginConst.XBDADMINT)
public class S100100101Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysMenuFacade sysMenuFacade;
	
	/**
	 * 菜单管理首页
	 * @return
	 */
	@RequestMapping("100100101")
    public String index(){
        return ROOT_PATH+"100100101";
    }
	
	/**
	 * 获取指定平台下的菜单树
	 * @return
	 */
	@RequestMapping("100100101100")
	@ResponseBody
	public TreeNodeResult getTree(long code){
		TreeNodeResult result = new TreeNodeResult();
		try{
			result = sysMenuFacade.queryMenuTree(code,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取平台所有菜单树异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 获取菜单中属于平台的菜单
	 * @return
	 */
	@RequestMapping("100100101100X")
	@ResponseBody
	public MenuPlatResult getAllPlatByMenu(){
		MenuPlatResult result = new MenuPlatResult();
		try{
			result = sysMenuFacade.getAllPlatByMenu(UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取平台所有平台菜单异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 新增菜单
	 * @return
	 */
	@RequestMapping("100100101101")
	@ResponseBody
	public XbdUnStringResult addSysMenu(SysMenuRequest request){
		XbdUnStringResult result = null;
		try{
			log.info("新增菜单,请求参数:{}", request);
			result = sysMenuFacade.addSysMenu(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result = new XbdUnStringResult();
			log.error("新增菜单失败,入参:{},异常:{}", request, e);;
		}
		return result;
	}
	
	/**
	 * 新增菜单选择平台列表
	 * @return
	 */
	@RequestMapping("100100101101X")
	@ResponseBody
	public PlatListResult getAllPlat(){
		PlatListResult result = new PlatListResult();
		try{
			result = sysMenuFacade.getAllPlat(UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取平台所有平台菜单异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 查看菜单详情
	 * @return
	 */
	@RequestMapping("100100101102X")
	@ResponseBody
	public SysMenuResult getMenuInfo(Long code){
		SysMenuResult result = new SysMenuResult();
		try{
			if (null==code||code.longValue()<0){
				throw BizException.PARAM_CHKE_FAIL.newInstance("菜单编号有误!");
			}
			log.info("查看菜单详情请求参数:{}",code);
			result = sysMenuFacade.getMenuInfo(code,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("查看菜单详情异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 修改菜单
	 * @return
	 */
	@RequestMapping("100100101102")
	@ResponseBody
	public XbdUnStringResult modifySysMenu(SysMenuRequest request){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("修改菜单传入参数:{}",request);
			if(null == request){
				throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象为空;");
			}
			result = sysMenuFacade.modifySysMenu(request,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("修改菜单异常:{}", e);
		}
		return result;
	}
	/**
	 * 删除菜单
	 * @return
	 */
	@RequestMapping("100100101103")
	@ResponseBody
	public XbdUnStringResult deleteSysMenu(Long code){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("删除菜单传入参数:{}",code);
			if(null == code){
				throw BizException.PARAM_CHKE_FAIL.newInstance("请求对象为空;");
			}
			if(code.longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("删除的菜单code不能为负");
			}
			result = sysMenuFacade.deleteSysMenu(code,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("删除菜单异常:{}", e);
		}
		return result;
	}
	
}
